<?php


namespace App\Component;


use Swoole\Table;

class SwooleTableUidBandServer implements UidBandServerInterface
{

    private Table $table;
    private string $column;

    /**
     * SwooleTableUidBandServer constructor.
     * @param Table $table swooletable
     * @param string $column 列值
     */
    public function __construct(Table $table, string $column)
    {
        $this->table = $table;
        $this->column = $column;
    }


    public function set($key, $value)
    {
        return $this->table->set((string)$key, [$this->column => $value]);
    }

    public function del($key)
    {
        return $this->table->del((string)$key);
    }

    public function exist($key)
    {
        return $this->table->exist((string)$key);
    }

    public function get($key)
    {
        return $this->table->get((string)$key)[$this->column] ?? false;
    }

    public function clear()
    {
        $keys = [];
        foreach ($this->table as $key => $value) {
            $keys[] = $key;
        }
        foreach ($keys as $key) {
            $this->table->del($key);
        }
    }
}